package edu.cmu.cs.stage3.alice.authoringtool.util;

import edu.cmu.cs.stage3.resourceBundle.I18n;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;

/**
 * @author Jason Pratt
 */

public class EditObjectButton extends javax.swing.JButton implements edu.cmu.cs.stage3.alice.authoringtool.util.GUIElement, edu.cmu.cs.stage3.alice.authoringtool.util.Releasable {

 protected AuthoringTool authoringTool;
 protected Object object;
 protected javax.swing.JComponent animationSource;

 public EditObjectButton () {
  setBackground( new java.awt.Color( 240, 240, 255 ) );
  setMargin( new java.awt.Insets( 0, 2, 0, 2 ) );
  setText(I18n.getString("msg_edit")); // "edit"
  int fontSize = 12;
  setFont( new java.awt.Font( "SansSerif", java.awt.Font.BOLD, (int)fontSize) );
  setFocusPainted( false );
  setBorder( new javax.swing.plaf.BorderUIResource.CompoundBorderUIResource( new CustomButtonBorder(), new javax.swing.plaf.basic.BasicBorders.MarginBorder() ) );
  addActionListener( new java.awt.event.ActionListener() {
   public void actionPerformed (java.awt.event.ActionEvent ev) {
    if (authoringTool != null) {
     if (object != null) {
      if (animationSource != null) {
       authoringTool.editObject( object, animationSource );
      } else {
       authoringTool.editObject( object );
       }
     } else {
      AuthoringTool.getInstance().showErrorDialog( "object unexpectedly null in EditObjectButton", null );
      }
    } else {
     AuthoringTool.getInstance().showErrorDialog( "authoringTool unexpectedly null in EditObjectButton", null );
     }
    }
   } );
  }

 public void setAuthoringTool (AuthoringTool authoringTool) {
  this.authoringTool = authoringTool;
  }

 public void setObject (Object object) {
  this.object = object;
  }

 public Object getObject () {
  return object;
  }

 public void setAnimationSource (javax.swing.JComponent animationSource) {
  this.animationSource = animationSource;
  }

 public void goToSleep () {}
 public void wakeUp () {}

 public void clean () {
  object = null;
  animationSource = null;
  }

 public void die () {
  clean();
  authoringTool = null;
  }

 public void release () {
  edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.releaseGUI( this );
  }

 class CustomButtonBorder extends javax.swing.border.AbstractBorder  implements javax.swing.plaf.UIResource {
  protected java.awt.Insets insets = new java.awt.Insets( 1, 3, 1, 3 );
  protected javax.swing.border.Border line = javax.swing.BorderFactory.createLineBorder( java.awt.Color.darkGray, 1 );
  protected javax.swing.border.Border spacer = javax.swing.BorderFactory.createEmptyBorder( 0, 3, 0, 3 );
  protected javax.swing.border.Border raisedBevel = new CustomBevelBorder( javax.swing.border.BevelBorder.RAISED );
  protected javax.swing.border.Border loweredBevel = new CustomBevelBorder( javax.swing.border.BevelBorder.LOWERED );
  protected javax.swing.border.Border raisedBorder = javax.swing.BorderFactory.createCompoundBorder( javax.swing.BorderFactory.createCompoundBorder( line, raisedBevel ), spacer );
  protected javax.swing.border.Border loweredBorder = javax.swing.BorderFactory.createCompoundBorder( javax.swing.BorderFactory.createCompoundBorder( line, loweredBevel ), spacer );
  //  protected javax.swing.border.Border raisedBorder = javax.swing.BorderFactory.createCompoundBorder( raisedBevel, spacer );
  //  protected javax.swing.border.Border loweredBorder = javax.swing.BorderFactory.createCompoundBorder( loweredBevel, spacer );

  //Rem @Override
  public void paintBorder (java.awt.Component c, java.awt.Graphics g, int x, int y, int w, int h) {
   javax.swing.JButton button = (javax.swing.JButton)c;
   javax.swing.ButtonModel model = button.getModel();

   if (model.isEnabled()) {
    if (model.isPressed() && model.isArmed()) {
     loweredBorder.paintBorder( button, g, x, y, w, h );
    } else {
     raisedBorder.paintBorder( button, g, x, y, w, h );
     }
   } else {
    raisedBorder.paintBorder( button, g, x, y, w, h );
    }
   }

  //Rem @Override
  public java.awt.Insets getBorderInsets (java.awt.Component c) {
   return insets;
   }
  }

 class CustomBevelBorder extends javax.swing.border.BevelBorder {
  public CustomBevelBorder (int type) {
   super( type );
   }
  //Rem @Override
  public java.awt.Color getHighlightInnerColor (java.awt.Component c) {
   return c.getBackground();
   }
  //Rem @Override
  public java.awt.Color getShadowInnerColor (java.awt.Component c) {
   return c.getBackground();
   }
  }

 }
